package zone.haiwen.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zone.haiwen.demo.util.JsonResult;
import zone.haiwen.demo.util.RedisUtil;
import zone.haiwen.demo.util.ResultCode;

import javax.annotation.Resource;


/**
 * Description
 * @author zhangHaiWen
 * @date 2021/7/8 13:10
 * @return
 **/
@RestController
public class StringRedisTestRest {

	protected static Logger logger = LoggerFactory.getLogger(StringRedisTestRest.class);

	@Resource
	private RedisTemplate<String, Object> redisTemplate;

    /**
	 * Description http://localhost:9090/set?key=name&value=minbo英文名
	 * @author zhangHaiWen
	 * @date 2021/7/8 11:22
	 * @return zone.haiwen.demo.util.JsonResult
	 **/
	@RequestMapping("/set")
	public JsonResult setKeyAndValue(String key, String value) {
		logger.debug("访问set:key={},value={}", key, value);
		RedisUtil.set(this.redisTemplate, key, value);
		return new JsonResult(ResultCode.SUCCESS);
	}

    /**
	 * Description: 例如：http://localhost:9090/get?key=name
	 * @author zhangHaiWen
	 * @date q 11:22
	 * @return zone.haiwen.demo.util.JsonResult
	 **/
	@RequestMapping("/get")
	public JsonResult getKey(String key) {
		logger.debug("访问get:key={}", key);
		String result = (String) RedisUtil.get(this.redisTemplate, key);
		logger.debug("get result=" + result);
		return new JsonResult(ResultCode.SUCCESS, result);
	}
}